package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.PermissionDao;
import com.itheima.domain.system.Permission;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.system.PermissionService;

import java.util.List;
import java.util.UUID;

/**
 * @Description:Service 实现类
 * @Param:
 * @return:
 * @Author:YangJinbo
 * @Date:2020/11/30
 */
public class PermissionServiceImpl implements PermissionService {
    /**
     * @Description:保存
     * @Param:[permission]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    @Override
    public void save(Permission permission) {
        PermissionDao permissionDao = DaoInstanceFactory.getMapper(PermissionDao.class);
        permission.setId(UUID.randomUUID().toString().replace("-", ""));
        permissionDao.save(permission);
    }

    /**
     * @Description:删除
     * @Param:[permission]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    @Override
    public void delete(Permission permission) {
        PermissionDao permissionDao = DaoInstanceFactory.getMapper(PermissionDao.class);
        permissionDao.delete(permission);
    }


    /**
     * @Description:查询所有
     * @Param:[pageNum, pageSize]
     * @return:com.github.pagehelper.PageInfo
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    @Override
    public PageInfo findAll(int pageNum, int pageSize) {
        PermissionDao permissionDao = DaoInstanceFactory.getMapper(PermissionDao.class);
        PageHelper.startPage(pageNum, pageSize);
        List<Permission> all = permissionDao.findAll();
        return new PageInfo<>(all);
    }


    /**
     * @Description:根据id删除
     * @Param:[ids]
     * @return:void
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    @Override
    public void deleteById(List<String> ids) {
        // 遍历数组
        if (ids != null) {
            for (String id : ids) {
                DaoInstanceFactory.getMapper(PermissionDao.class).deleteById(id);
            }
        }
    }

    /**
     * @Description:根据id查询
     * @Param:[id]
     * @return:com.itheima.domain.system.Permission
     * @Author:YangJinbo
     * @Date:2020/12/1
     */
    @Override
    public Permission findById(String id) {
        PermissionDao permissionDao = DaoInstanceFactory.getMapper(PermissionDao.class);
        Permission permission = new Permission();
        return permissionDao.findById(id);
    }


    /**
     * @Description:根据权限名删除
     * @Param:[permissionName]
     * @return:java.util.List<com.itheima.domain.system.Permission>
     * @Author:YangJinbo
     * @Date:2020/11/30
     */
    @Override
    public List<Permission> findAllByPermissionName(String permissionName) {
        PermissionDao permissionDao = DaoInstanceFactory.getMapper(PermissionDao.class);
        List<Permission> permissionList = null;
        try {
            permissionList = permissionDao.findAllByPermissionName(permissionName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return permissionList;
    }
}
